package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.article.feign.BehaviorFeign;
import com.usian.article.mapper.ApCollectionMapper;
import com.usian.article.mapper.ArticleMapper;
import com.usian.article.service.ApCollectionService;
import com.usian.model.article.ApArticle;
import com.usian.model.article.ApCollection;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.utils.threadlocal.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApCollectionServiceImpl extends BaseServiceImpl<ApCollectionMapper, ApCollection> implements ApCollectionService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private BehaviorFeign behaviorFeign;
    @Override
    public ResponseResult collection(Long articleId,Integer status) {
        if (articleId == null)return paramError;
        Integer userId = ThreadLocalUtils.getUserId();
        ApBehaviorEntry entry = behaviorFeign.getEntry(userId);
        if (status == 0){//收藏
            ApCollection collection = new ApCollection();
            collection.setCollectionTime(new Date());
            collection.setArticleId(articleId);
            Date publishTime = articleMapper.selectById(articleId).getPublishTime();
            collection.setPublishedTime(publishTime);
            collection.setType((short) 0);
            collection.setEntryId(entry.getId());
            save(collection);
        } else {
            remove(Wrappers.<ApCollection>lambdaQuery().eq(ApCollection::getArticleId,articleId).eq(ApCollection::getEntryId,entry.getId()));
        }
        return success;
    }
}
